package org.example.flex.controller;

import com.mybatisflex.core.paginate.Page;
import org.example.mybatis.entity.Course;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.example.flex.service.CourseService;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;

import java.io.Serializable;
import java.util.List;

/**
 * 课程管理控制器
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@RestController
@RequestMapping("/course")
@Tag(name = "课程管理", description = "课程信息的增删改查操作")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 添加课程
     *
     * @param course 课程信息
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("/save")
    @Operation(summary = "创建课程", description = "添加新的课程信息")
    public boolean save(@RequestBody Course course) {
        return courseService.save(course);
    }


    /**
     * 根据主键删除
     *
     * @param id 课程ID
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("/remove/{id}")
    @Operation(summary = "删除课程", description = "根据课程ID删除课程信息")
    public boolean remove(@Parameter(description = "课程ID", example = "1001", required = true) 
                         @PathVariable Serializable id) {
        return courseService.removeById(id);
    }


    /**
     * 根据主键更新
     *
     * @param course 课程信息
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("/update")
    @Operation(summary = "更新课程", description = "根据课程ID更新课程信息")
    public boolean update(@RequestBody Course course) {
        return courseService.updateById(course);
    }


    /**
     * 查询所有
     *
     * @return 所有数据
     */
    @GetMapping("/list")
    @Operation(summary = "查询所有课程", description = "获取所有课程信息的列表")
    public List<Course> list() {
        return courseService.list();
    }


    /**
     * 根据主键获取详细信息。
     *
     * @param id course主键
     * @return 详情
     */
    @GetMapping("/getInfo/{id}")
    @Operation(summary = "获取课程详情", description = "根据课程ID获取课程的详细信息")
    public Course getInfo(
            @Parameter(description = "课程ID", example = "1001", required = false)
            @PathVariable("id") Integer id)
    {
        return courseService.getById(id);
    }


    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询课程", description = "根据分页参数查询课程列表")
    public Page<Course> page(@Parameter(description = "分页参数，包含页码和每页大小", example = "{\"pageNumber\":1,\"pageSize\":10}") 
                            Page<Course> page) {
        return courseService.page(page);
    }
}